package com.heima.common.exception;

import cn.hutool.core.util.ObjectUtil;
import com.heima.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandle {

    //处理自定义异常 返回自定义异常中的 code码 和信息
    @ExceptionHandler(BaseException.class)
    public Result handleBaseException(BaseException e){
        e.printStackTrace();
        if(ObjectUtil.isNotEmpty(e.getBasicEnum())){
            log.error("自定义异常：{}",e.getBasicEnum().getMsg());
        }
        return Result.error(e.getBasicEnum());
    }

    //处理其他异常
    @ExceptionHandler(Exception.class)
    public Result handleUnknownException(Exception e){
        e.printStackTrace();
        if(ObjectUtil.isNotEmpty(e.getCause())){
            log.error("其他未知异常：{}",e.getMessage());
        }
        return Result.error(500,"系统异常");
    }

}
